var TableSale = function () {

    var initTable = function(nodeId) {

        /* Formating function for row details */
        function fnFormatDetails ( oTable, nTr )
        {
            var aData = $(nTr).find("div[name=feature]").children();
            var sOut = '<table>';
            sOut += '<tr><td>菜品:</td><td>'+$(aData[0]).text()+'</td></tr>';
            sOut += '<tr><td>地区:</td><td>'+$(aData[1]).text()+'</td></tr>';
            sOut += '<tr><td>开始时间:</td><td>'+$(aData[2]).text()+'</td></tr>';
            sOut += '<tr><td>结束时间:</td><td>'+$(aData[3]).text()+'</td></tr>';
            sOut += '</table>';
             
            return sOut;
        }

        /*
         * Insert a 'details' column to the table
         */
        var nCloneTh = document.createElement( 'th' );
        var nCloneTd = document.createElement( 'td' );
        nCloneTd.innerHTML = '<span class="row-details row-details-close"></span>';
        
        $('#' + nodeId + ' thead tr').each( function () {
            this.insertBefore( nCloneTh, this.childNodes[0] );
        } );
         
        $('#' + nodeId + ' tbody tr').each( function () {
            this.insertBefore(  nCloneTd.cloneNode( true ), this.childNodes[0] );
        } );
         
        /*
         * Initialse DataTables, with no sorting on the 'details' column
         */
        var oTable = $('#' + nodeId).dataTable( {
            'bSort': false,
            "aaSorting": [[1, 'asc']],
            "bStateSave" : true,
             "aLengthMenu": [
                [5, 10, 20, -1],
                [5, 10, 20, "所有记录"] // change per page values here
            ],
            "oLanguage": {
            	"sLengthMenu": "每页显示 _MENU_ 条记录",
				"sZeroRecords": "抱歉， 没有找到相应记录",
				"sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
				"sInfoEmpty": "没有数据",
				"sInfoFiltered": "(从 _MAX_ 条数据中检索)",
				"oPaginate": {
					"sFirst": "首页",
					"sPrevious": "前一页",
					"sNext": "后一页",
					"sLast": "尾页"
				},
            	"sSearch": "搜索:",
            },
            "sZeroRecords": "没有检索到数据",
            // set the initial value
            "iDisplayLength": 10,
        });

        jQuery('#' + nodeId + '_wrapper .dataTables_filter input').addClass("m-wrap small"); // modify table search input
        jQuery('#' + nodeId + '_wrapper .dataTables_length select').addClass("m-wrap small"); // modify table per page dropdown
        
        $('#' + nodeId + '_wrapper .dataTables_filter input').keyup(function(){
        	oTable.fnFilter(this.value, 3);
        });
        /* Add event listener for opening and closing details
         * Note that the indicator for showing which row is open is not controlled by DataTables,
         * rather it is done here
         */
        $('#' + nodeId).on('click', ' tbody td .row-details', function () {
            var nTr = $(this).parents('tr')[0];
            if ( oTable.fnIsOpen(nTr) )
            {
                /* This row is already open - close it */
                $(this).addClass("row-details-close").removeClass("row-details-open");
                oTable.fnClose( nTr );
            }
            else
            {
                /* Open this row */                
                $(this).addClass("row-details-open").removeClass("row-details-close");
                oTable.fnOpen( nTr, fnFormatDetails(oTable, nTr), 'details' );
            }
        });
    };

    return {

        //main function to initiate the module
        init: function (nodeId) {
            
            if (!jQuery().dataTable) {
                return;
            }
			
            initTable(nodeId);
        }

    };

}();